<template>
  <a-layout class="admin-layout">
    <!-- 侧边栏 -->
    <a-layout-sider
      v-model:collapsed="collapsed"
      :trigger="null"
      collapsible
      class="admin-sider"
      :width="256"
    >
      <div class="logo">
        <h2>蜗牛AI管理</h2>
      </div>
      <div class="menu-container">
        <a-menu
          v-model:selectedKeys="selectedKeys"
          theme="dark"
          mode="inline"
          @click="handleMenuClick"
          class="admin-menu"
        >
          <a-menu-item key="dashboard">
            <template #icon>
              <DashboardOutlined />
            </template>
            <span>数据概览</span>
          </a-menu-item>
          <a-menu-item key="permissions">
          <template #icon>
          <LockOutlined />  
        </template>
        <span>菜单权限管理</span>
        </a-menu-item>
          <a-menu-item key="courses">
            <template #icon>
              <BookOutlined />
            </template>
            <span>课程管理</span>
          </a-menu-item>
          <a-menu-item key="videos">
            <template #icon>
              <VideoCameraOutlined />
            </template>
            <span>视频管理</span>
          </a-menu-item>
          <a-menu-item key="users">
            <template #icon>
              <UserOutlined />
            </template>
            <span>用户管理</span>
          </a-menu-item>
          <a-menu-item key="assignments">
            <template #icon>
              <FileTextOutlined />
            </template>
            <span>作业管理</span>
          </a-menu-item>
          <a-menu-item key="exams">
            <template #icon>
              <FormOutlined />
            </template>
            <span>考试管理</span>
          </a-menu-item>
          <a-menu-item key="orders">
            <template #icon>
              <ShoppingOutlined />
            </template>
            <span>订单管理</span>
          </a-menu-item>
          <a-menu-item key="task-management">
            <template #icon>
              <ShoppingOutlined />
            </template>
            <span>任务管理</span>
          </a-menu-item>
          <a-menu-item key="settings">
            <template #icon>
              <SettingOutlined />
            </template>
            <span>系统设置</span>
          </a-menu-item>
        </a-menu>
      </div>
    </a-layout-sider>

    <!-- 主布局 -->
    <a-layout class="main-layout">
      <!-- 顶部导航 -->
      <a-layout-header class="admin-header">
        <div class="header-left">
          <a-button type="text" @click="collapsed = !collapsed" class="collapse-btn">
            <MenuUnfoldOutlined v-if="collapsed" />
            <MenuFoldOutlined v-else />
          </a-button>
          <a-breadcrumb class="breadcrumb">
            <a-breadcrumb-item>管理后台</a-breadcrumb-item>
            <a-breadcrumb-item>{{ currentPageTitle }}</a-breadcrumb-item>
          </a-breadcrumb>
        </div>
        <div class="header-right">
          <a-dropdown>
            <a-button type="text" class="user-dropdown">
              <UserOutlined />
              管理员
              <DownOutlined />
            </a-button>
            <template #overlay>
              <a-menu>
                <a-menu-item @click="handleLogout">
                  <LogoutOutlined />
                  退出登录
                </a-menu-item>
              </a-menu>
            </template>
          </a-dropdown>
        </div>
      </a-layout-header>

      <!-- 主内容区域 -->
      <a-layout-content class="admin-content">
        <div class="content-wrapper">
          <router-view />
        </div>
      </a-layout-content>
    </a-layout>
  </a-layout>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { message } from 'ant-design-vue'
import { logout } from '@/api/loginController'
import {
  DashboardOutlined,
  BookOutlined,
  UserOutlined,
  FileTextOutlined,
  FormOutlined,
  ShoppingOutlined,
  SettingOutlined,
  MenuUnfoldOutlined,
  MenuFoldOutlined,
  DownOutlined,
  LogoutOutlined,
  VideoCameraOutlined,
  LockOutlined
} from '@ant-design/icons-vue'

const router = useRouter()
const route = useRoute()

// 响应式数据
const collapsed = ref(false)
const selectedKeys = ref(['dashboard'])

// 响应式处理
const handleResize = () => {
  if (window.innerWidth <= 768) {
    collapsed.value = true
  }
}

// 计算属性
const currentPageTitle = computed(() => {
  const pageTitles = {
    adminDashboard: '数据概览',
    adminCourses: '课程管理',
    adminUsers: '用户管理',
    adminVideos: '视频管理',
    adminAssignments: '作业管理',
    adminExams: '考试管理',
    adminOrders: '订单管理',
    adminSettings: '系统设置',
    adminPermissions: '菜单权限管理'
  }
  return pageTitles[route.name as keyof typeof pageTitles] || '管理后台'
})

// 方法
const handleMenuClick = ({ key }: { key: string }) => {
  selectedKeys.value = [key]
  router.push(`/admin/${key === 'dashboard' ? '' : key}`)
}

const handleLogout = async () => {
  try {

    await logout(); 
    
    // 清除前端存储的token和用户信息
    localStorage.removeItem('token');
    localStorage.removeItem('userInfo');

    // 跳转到登录页
    router.push('/login');
    message.success('退出登录成功');
  } catch (error) {
    console.error('退出登录失败:', error);
    message.error('退出登录失败，请重试');
  }
};

// 根据当前路由设置选中的菜单项
const updateSelectedKeys = () => {
  const path = route.path
  if (path === '/admin') {
    selectedKeys.value = ['dashboard']
  } else {
    const key = path.split('/').pop()
    if (key) {
      selectedKeys.value = [key]
    }
  }
}

// 监听路由变化
import { watch, onMounted, onUnmounted } from 'vue'

watch(
  () => route.path,
  () => {
    updateSelectedKeys()
  },
  { immediate: true },
)

// 监听窗口大小变化
onMounted(() => {
  handleResize()
  window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
  window.removeEventListener('resize', handleResize)
})
</script>

<style scoped>
.admin-layout {
  height: 100vh;
  overflow: hidden;
}

/* 侧边栏样式 */
.admin-sider {
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  z-index: 1000;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.15);
  transition: all 0.2s ease;
}

.logo {
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 16px;
  font-weight: bold;
  border-bottom: 1px solid #001529;
  background: #001529;
}

.logo h2 {
  color: #fff;
  margin: 0;
  font-size: 18px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.menu-container {
  height: calc(100vh - 64px);
  overflow-y: auto;
  overflow-x: hidden;
}

.admin-menu {
  border-right: none;
  height: 100%;
}

/* 主布局样式 */
.main-layout {
  margin-left: 256px;
  transition: margin-left 0.2s ease;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

/* 侧边栏收起时的主布局 */
.admin-layout.ant-layout-sider-collapsed .main-layout {
  margin-left: 80px;
}

/* 顶部导航样式 */
.admin-header {
  position: fixed;
  top: 0;
  right: 0;
  left: 256px;
  z-index: 999;
  background: #fff;
  padding: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: left 0.2s ease;
  height: 64px;
}

/* 侧边栏收起时的顶部导航 */
.admin-layout.ant-layout-sider-collapsed .admin-header {
  left: 80px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 16px;
  padding-left: 16px;
}

.collapse-btn {
  font-size: 16px;
  width: 64px;
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  transition: all 0.3s ease;
}

.collapse-btn:hover {
  background: #f5f5f5;
  color: #1890ff;
}

.breadcrumb {
  font-size: 14px;
}

.header-right {
  padding-right: 24px;
}

.user-dropdown {
  height: 64px;
  display: flex;
  align-items: center;
  padding: 0 16px;
  border: none;
  background: transparent;
  transition: all 0.3s ease;
}

.user-dropdown:hover {
  background: #f5f5f5;
  color: #1890ff;
}

/* 主内容区域样式 */
.admin-content {
  margin-top: 64px;
  height: calc(100vh - 64px);
  overflow: hidden;
  background: #f0f2f5;
  padding: 0;
}

.content-wrapper {
  height: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 24px;
}

/* 滚动条样式 */
.menu-container::-webkit-scrollbar,
.content-wrapper::-webkit-scrollbar {
  width: 6px;
}

.menu-container::-webkit-scrollbar-track,
.content-wrapper::-webkit-scrollbar-track {
  background: transparent;
}

.menu-container::-webkit-scrollbar-thumb,
.content-wrapper::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.2);
  border-radius: 3px;
}

.menu-container::-webkit-scrollbar-thumb:hover,
.content-wrapper::-webkit-scrollbar-thumb:hover {
  background: rgba(0, 0, 0, 0.3);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .admin-sider {
    transform: translateX(-100%);
  }

  .admin-sider.ant-layout-sider-collapsed {
    transform: translateX(0);
  }

  .main-layout {
    margin-left: 0;
  }

  .admin-header {
    left: 0;
  }
}

/* 菜单项悬停效果 */
.admin-menu .ant-menu-item {
  transition: all 0.3s ease;
}

.admin-menu .ant-menu-item:hover {
  background: rgba(255, 255, 255, 0.1);
  transform: translateX(4px);
}

.admin-menu .ant-menu-item-selected {
  background: #1890ff !important;
  box-shadow: 0 2px 8px rgba(24, 144, 255, 0.3);
}

/* 内容区域卡片样式 */
.content-wrapper :deep(.ant-card) {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border-radius: 8px;
  margin-bottom: 24px;
}

.content-wrapper :deep(.ant-card-head) {
  border-bottom: 1px solid #f0f0f0;
  background: #fafafa;
}

/* 表格样式优化 */
.content-wrapper :deep(.ant-table) {
  border-radius: 8px;
  overflow: hidden;
}

.content-wrapper :deep(.ant-table-thead > tr > th) {
  background: #fafafa;
  font-weight: 600;
}

/* 按钮样式优化 */
.content-wrapper :deep(.ant-btn) {
  border-radius: 6px;
  transition: all 0.3s ease;
}

.content-wrapper :deep(.ant-btn:hover) {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

/* 表单样式优化 */
.content-wrapper :deep(.ant-form-item-label > label) {
  font-weight: 500;
  color: #333;
}

.content-wrapper :deep(.ant-input),
.content-wrapper :deep(.ant-select-selector) {
  border-radius: 6px;
  transition: all 0.3s ease;
}

.content-wrapper :deep(.ant-input:focus),
.content-wrapper :deep(.ant-select-focused .ant-select-selector) {
  border-color: #1890ff;
  box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
}
</style>
